//
//  InformationViewController.m
//  cityCar
//
//  Created by yao on 14-11-3.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "InformationViewController.h"
#import "PullView.h"
#import "MarketPullView.h"
#import "AutoScrollView.h"
#import "DWBubbleMenuButton.h"

#import "InformationCollectionViewCell.h"
#import "HomeCollectionViewCell.h"
#import "TushangCollectionViewCell.h"
#import "MarketCollectionViewCell.h"

#import "CKWabViewController.h"
#import "TuShangShowViewController.h"
#import "AreaViewController.h"
#import "CKCar.h"
#import "CKPhoto.h"
#import "CityName.h"

@interface InformationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *bottomView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, assign)NSInteger count;
@property (nonatomic, retain)NSTimer *ti;
@end

@implementation InformationViewController

- (void)dealloc
{
    [_bottomView release];
    [_array release];
    [_scrollView release];
    // 移除消息中心
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_ti release];
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.array = [NSMutableArray array];
        self.count = 0;
    }
    return self;
}

- (void)viewDidLoad
{
    self.ti = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(animationAction:) userInfo:nil repeats:YES];
    [super viewDidLoad];
    self.navigationItem.title = @"都市汽车";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    // Do any additional setup after loading the view.
    [self createCollectionView];
    UILabel *homeLabel = [self createHomeButtonView];
    
    //侧边下拉按钮
    DWBubbleMenuButton *button = [[DWBubbleMenuButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 35, 64, homeLabel.frame.size.width, homeLabel.frame.size.height) expansionDirection:DirectionDown];
    button.homeButtonView = homeLabel;
    if (self.view.frame.size.height < 481) {
        button.buttonSpacing = 3;
    } else {
        button.buttonSpacing = 10;
    }
    [button addButtons:[self createDemoButtonArray]];
    [self.view addSubview:button];
    
    
}
// 上侧按钮动画
- (void)animationAction:(NSTimer *)ti
{
    CGPoint newOffset = _scrollView.contentOffset;
    
    if (_count < _scrollView.contentSize.width * 2) {
        
        if (_count < _scrollView.contentSize.width) {
            newOffset.x += 20;
        } else {
            newOffset.x -= 20;
        }
        _count += 20;
    } else {
        _count = 0;
    }
    [self.scrollView setContentOffset:newOffset animated:YES];
}

#pragma mark 侧边按钮的视图创建
- (UILabel *)createHomeButtonView
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    
    label.text = @"更多";
    label.font = [UIFont boldSystemFontOfSize:13];
    label.textColor = [WebColor white];
    label.textAlignment = NSTextAlignmentCenter;
    //    label.layer.cornerRadius = 17.5;
    label.backgroundColor = [UIColor lightGrayColor];
    label.clipsToBounds = YES;
    //    [label sizeToFit];
    return label;
}

- (NSArray *)createDemoButtonArray
{
    NSMutableArray *buttonsMutable = [[NSMutableArray alloc] init];
    int i = 0;
    for (NSString *title in @[@"首页", @"新车", @"行情", @"导购", @"评测", @"视频", @"图赏", @"用车", @"游记", @"行业", @"赛事", @"技术"]) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setTitleColor:[WebColor white] forState:UIControlStateNormal];
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:10];
        button.frame = CGRectMake(0, 0, 24, 24);
        button.layer.cornerRadius = 12;
        button.backgroundColor = [WebColor dimGray];
        button.clipsToBounds = YES;
        button.tag = i ++;
        
        [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
        [buttonsMutable addObject:button];
    }
    return [buttonsMutable copy];
}

// 侧面按钮的点击方法
- (void)test:(UIButton *)sender
{
    //    NSLog(@"tag: %d", sender.tag);
    [self.bottomView setContentOffset:CGPointMake(self.view.frame.size.width * sender.tag, -64) animated:YES];
    // 上面按钮高亮状态
    NSLog(@"%@", [self.scrollView subviews]);
    for (int i = 0; i < 12; i ++) {
        UIButton *but = (UIButton *)[self.scrollView viewWithTag:23230 + i];
        [but setBackgroundColor:[UIColor whiteColor]];
        [but setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    UIButton *button = (UIButton *)[self.scrollView viewWithTag:(sender.tag + 23230)];
    [button setBackgroundColor:[UIColor lightGrayColor]];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
}
#pragma mark -
// 创建最大的集合视图
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width - 1, self.view.frame.size.height- 64 - 44 - 40);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumLineSpacing = 1;
    
    self.bottomView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 26, self.view.frame.size.width, self.view.frame.size.height - 20) collectionViewLayout:flowLayout];
    
    _bottomView.backgroundColor = [UIColor whiteColor];
    _bottomView.pagingEnabled = YES;
    _bottomView.delegate = self;
    _bottomView.dataSource = self;
    [_bottomView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"CK_homePage"];
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_newCar"];
    
    [_bottomView registerClass:[MarketCollectionViewCell class] forCellWithReuseIdentifier:@"CK_market"];
    
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_shoppers"];
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_review"];
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_video"];
    
    [_bottomView registerClass:[TushangCollectionViewCell class] forCellWithReuseIdentifier:@"CK_tuShang"];
    
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_theCar"];
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_travel"];
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_industry"];
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_event"];
    [_bottomView registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"CK_technology"];
    
    
    [self.view addSubview:_bottomView];
    [_bottomView release];
    
    // 上测按钮的创建
    self.scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:_scrollView];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [_scrollView release];
    self.scrollView.delegate = self;
    CGFloat w = self.view.frame.size.width;
    self.scrollView.frame = CGRectMake(0, 64, w - 35, 35);
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentSize = CGSizeMake(w * 3, 0);
    
    NSArray *typeArray = [NSArray arrayWithObjects:@"首页", @"新车", @"行情", @"导购", @"评测", @"视频", @"图赏", @"用车", @"游记", @"行业", @"赛事", @"技术", nil];
    for (int i = 0; i < typeArray.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
        if (i == 0) {
            [button setBackgroundColor:[UIColor lightGrayColor]];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
        } else {
            [button setBackgroundColor:[UIColor whiteColor]];
            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
        button.tag = i + 23230;
        [button setFrame:CGRectMake(w / 4 * i + 5, 5, w / 4 - 5, 25)];
        [button setTitle:[typeArray objectAtIndex:i] forState:UIControlStateNormal];
        button.layer.cornerRadius = 10;
        button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [button addTarget:self action:@selector(typeButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
}
// 上面滚动按钮的实现
- (void)typeButton:(UIButton *)button
{
    // 偏移量
    [self.bottomView setContentOffset:CGPointMake(self.view.frame.size.width * (button.tag - 23230), -64) animated:YES];
    // 按钮的高亮状态
    for (int i = 0; i < 12; i ++) {
        UIButton *but = (UIButton *)[self.scrollView viewWithTag:23230 + i];
        [but setBackgroundColor:[UIColor whiteColor]];
        [but setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    [button setBackgroundColor:[UIColor lightGrayColor]];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
}


#pragma mark -
#pragma mark 集合视图协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.row) {
        case 0:
        {
            HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_homePage" forIndexPath:indexPath];
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                ckWabView.car = str;
                
                [ckWabView release];
            };
            cell.waterfallView.block = blockValue;
            
            void (^shuffling)(CKPhoto *) = ^(CKPhoto *ckPhoto){
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                ckWabView.car = (CKCar *)ckPhoto;
                
                [ckWabView release];
            };
            cell.shuffling = shuffling;
            
            cell.backgroundColor = [UIColor whiteColor];
            return cell;
        }
            break;
        case 1:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_newCar" forIndexPath:indexPath];
            
            // 传入网址
            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/2?pageNo=1&pageSize=20&v=4.0.0";
            
            // 传出id并跳转
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                CATransition *animation=[CATransition animation];
                animation.delegate=self;
                animation.duration=0.3;
                animation.type = kCATransitionPush;
                animation.subtype = kCATransitionFromTop;
                [self.navigationController.view.layer addAnimation:animation forKey:@"animation"];
                
                [self.navigationController pushViewController:ckWabView animated:NO];
                
                ckWabView.car = str;
                
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            return cell;
        }
            break;
        case 2:// 行情
        {
            MarketCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_market" forIndexPath:indexPath];
            // 先从本地读取
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentPath = [paths lastObject];
            NSString *arrPath = [documentPath stringByAppendingPathComponent:@"area.xml"];
            NSArray *readArr = [NSArray arrayWithContentsOfFile:arrPath];
            NSLog(@"读取本地地区：%@", readArr);
            
            // 判断有就先从本地读取~
            if (readArr == nil) {
                cell.pullView.province = @"辽宁";
                cell.pullView.city = @"大连";
            } else {
                cell.pullView.province = [readArr firstObject];
                cell.pullView.city = [readArr lastObject];
            }
            
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                [self.navigationController pushViewController:ckWabView animated:YES];
                
                ckWabView.car = str;
                
                [ckWabView release];
                
            };
            cell.pullView.block = blockValue;
            
            // 跳到选择城市页
            void (^cityView)(NSString *str) = ^(NSString *str){
                
                AreaViewController *areaView = [[AreaViewController alloc]init];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                [self.navigationController pushViewController:areaView animated:YES];

                [areaView release];
                
            };
            cell.block = cityView;
            
            
            return cell;
        }
            
            break;
        case 3:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_shoppers" forIndexPath:indexPath];

            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/3?pageNo=1&pageSize=20&v=4.0.0";
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                ckWabView.car = str;
                
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            
            return cell;
        }
            break;
        case 4:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_review" forIndexPath:indexPath];

            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/4?pageNo=1&pageSize=20&v=4.0.0";
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                ckWabView.car = str;
                
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            
            return cell;
        }
            break;
        case 5:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_video" forIndexPath:indexPath];

            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/13?pageNo=1&pageSize=20&v=4.0.0";
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                ckWabView.car = str;
                
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            
            
            return cell;
        }
            break;
        case 6:
        {
            TushangCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_tuShang" forIndexPath:indexPath];
            void (^blockValue)(NSString *, NSString *) = ^(NSString *str, NSString *str1){
                
                TuShangShowViewController *imageShow = [[TuShangShowViewController alloc]init];
                [self.navigationController pushViewController:imageShow animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                imageShow.urlStr = str;
                imageShow.pageCount = str1;
                [imageShow release];
                
            };
            cell.block = blockValue;
            return cell;
        }
            break;
        case 7:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_theCar" forIndexPath:indexPath];

            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/5?pageNo=1&pageSize=20&v=4.0.0";
            
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                ckWabView.car = str;
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            
            return cell;
        }
            break;
        case 8:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_travel" forIndexPath:indexPath];

            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/15?pageNo=1&pageSize=20&v=4.0.0";
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                ckWabView.car = str;
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            
            return cell;
        }
            break;
        case 9:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_industry" forIndexPath:indexPath];

            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/10?pageNo=1&pageSize=20&v=4.0.0";
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                ckWabView.car = str;
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            
            return cell;
        }
            break;
        case 10:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_event" forIndexPath:indexPath];

            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/16?pageNo=1&pageSize=20&v=4.0.0";
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                ckWabView.car = str;
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            return cell;
        }
            break;
        default:
        {
            InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_technology" forIndexPath:indexPath];
            
            cell.water.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/8?pageNo=1&pageSize=20&v=4.0.0";
            void (^blockValue)(CKCar *str) = ^(CKCar *str){
                
                CKWabViewController *ckWabView = [[CKWabViewController alloc]init];
                [self.navigationController pushViewController:ckWabView animated:YES];
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                [self.tabBarController.tabBar setHidden:YES];
                
                ckWabView.car = str;
                
                [ckWabView release];
                
            };
            cell.water.block = blockValue;
            
            return cell;
        }
            break;
    }
    
}

#pragma mark - 
#pragma mark 滚动偏移量判断
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //    NSLog(@"%s", __func__);
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //    NSLog(@"%s", __func__);
    if (scrollView == self.scrollView) {
        if (self.scrollView.contentOffset.x > self.scrollView.contentSize.width - 70 || self.scrollView.contentOffset.x < -10) {
            _count = 0;
            [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y) animated:YES];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //    NSLog(@"%s", __func__);
    //    [self.ti invalidate];
    if (scrollView == _bottomView) {
        NSInteger tag = (_bottomView.contentOffset.x + 10)/ self.bottomView.frame.size.width;
        for (int i = 0; i < 12; i ++) {
            UIButton *but = (UIButton *)[self.scrollView viewWithTag:23230 + i];
            [but setBackgroundColor:[UIColor whiteColor]];
            [but setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
        UIButton *button = (UIButton *)[self.scrollView viewWithTag:(tag + 23230)];
        [button setBackgroundColor:[UIColor lightGrayColor]];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        //        [_scrollView setContentOffset:CGPointMake(button.frame.size.width * tag - 110, 0) animated:YES];
    }
    
    //    self.ti = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(animationAction:) userInfo:nil repeats:YES];
    
}
#pragma mark - 

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
